﻿/** 
 * Description:登录控制
 * Date:2011-7-18
 * Author:Along	
*/
package com.community.shopping.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.community.shopping.domain.User;
import com.community.shopping.service.logic.UserManager;
import com.community.shopping.util.RequestHelp;


@Controller 
public class LoginControl {
	

	@Autowired  
    private UserManager userManager;
	
	//用户注册
	@RequestMapping("/login/addUser")
	 public void  addUser(HttpServletRequest request,HttpServletResponse response)throws Exception 
	 {
		request.getSession();
	 } 
	
	//检查帐号是否使用过
	@RequestMapping("/login/checkTel")
	 public void  checkTel(HttpServletRequest request,HttpServletResponse response)throws Exception{ 
      String tel=request.getParameter("tel");
      if(userManager.checkTel(tel))
      {
    	  response.getWriter().print("true"); 		    	  
      }else
      {
    	  response.getWriter().print("false"); 		
      }
	  } 
	
	//用户登录验证
	@RequestMapping("/login/checkUser")
	 public void  checkUser(HttpServletRequest request,HttpServletResponse response)throws Exception{ 
	  String tel=request.getParameter("tel").toString();
	  String password=request.getParameter("password").toString(); 
	  if(userManager.checkUser(tel, password,request))
	  {
		  User user=(User)request.getSession().getAttribute("user");
		  response.setCharacterEncoding("UTF-8");
		  response.getWriter().print(
				  "{\"id\":\""+user.getId()+"\",\"username\":\""+user.getUserName()+"\",\"password\":\""+user.getPassWord()+"\",\"relname\":\""+user.getRealName()+"\",\"address\":\""+user.getAddress()+"\",\"score\":\""+user.getScore()+"\"}"); 	  		  
	  }else
      {
    	  response.getWriter().print("false"); 		
      }
	  }
	
	//用户注册
	@RequestMapping("/login/reg")
	 public void  regUser(HttpServletRequest request,HttpServletResponse response)throws Exception{ 
	  String tel=request.getParameter("tel");
	  if(userManager.checkTel(tel))
      {
    	  response.getWriter().print("false"); 		    	  
      }else
      {
    	  String password=request.getParameter("password"); 
    	  String relname=request.getParameter("relname"); 
    	  relname=java.net.URLDecoder.decode(relname, "UTF-8");  
    	  String address=request.getParameter("address"); 
    	  address=java.net.URLDecoder.decode(address, "UTF-8");  
    	  userManager.addUser(tel, password, relname, address, request);
    	  User user=(User)request.getSession().getAttribute("user");
		  response.setCharacterEncoding("UTF-8");
		  response.getWriter().print(
				  "{\"id\":\""+user.getId()+"\",\"username\":\""+user.getUserName()+"\",\"relname\":\""+user.getRealName()+"\",\"address\":\""+user.getAddress()+"\",\"score\":\""+user.getScore()+"\"}"); 	  		  		
      }
	 
	  }
	
	//修改用户信息
	@RequestMapping("/login/update")
	 public void  updateUser(HttpServletRequest request,HttpServletResponse response)throws Exception{ 
	      String userid=request.getParameter("id");	 
	      User user=userManager.getUserById(userid);	     	      
    	  String password=request.getParameter("password"); 
    	  if(password!=null && !password.equals(""))
    	  {
    	  password=java.net.URLDecoder.decode(password, "UTF-8");  
    	  user.setPassWord(password);
    	  }
    	  String relname=request.getParameter("relname"); 
    	  relname=java.net.URLDecoder.decode(relname, "UTF-8");  
    	  String address=request.getParameter("address"); 
    	  address=java.net.URLDecoder.decode(address, "UTF-8");      	 
    	  user.setRealName(relname);
    	  user.setAddress(address);
    	  userManager.save(user);
    	  request.getSession().setAttribute("user", user);
		  response.setCharacterEncoding("UTF-8");
		  response.getWriter().print(
				  "{\"id\":\""+user.getId()+"\",\"username\":\""+user.getUserName()+"\",\"relname\":\""+user.getRealName()+"\",\"address\":\""+user.getAddress()+"\",\"score\":\""+user.getScore()+"\"}"); 	  		  		
   
	 
	  }
	
	//后台登录验证
	@RequestMapping("/login/admin")
	 public  ModelAndView   admin(HttpServletRequest request,HttpServletResponse response)throws Exception{
	   ModelAndView modelAndView = new ModelAndView("redirect:/admin/orderManage.do"); 
	   String username=request.getParameter("username");
	   String password=request.getParameter("password"); 
	   String captcha = (String) request.getParameter("captcha");
	   String c = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		
	   if(captcha.equals(c)&&"admin".equals(username)&&"ziheng".equals(password))
	   {
	   request.getSession().setAttribute(RequestHelp.SESSION_SHOPID,"1");
	   }
	   else
		   modelAndView.setViewName("/login.jsp");
	   return modelAndView;
	  }
}
